package cn.swpu.blog.controller;

import cn.swpu.blog.entity.Category;
import cn.swpu.blog.service.ICategoryService;
import cn.swpu.blog.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author 冰翔
 * @Date Created in 3:26 下午 2020/12/11
 */
@Api(tags = "类别Category")
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @PreAuthorize("permitAll()")
    @ApiOperation("根据Id查询类别内容")
    @GetMapping("/{categoryId}")
    public ResponseVo<Map<String, String>> queryCategory(@PathVariable Integer categoryId){
        return categoryService.queryCategoryById(categoryId);
    }

    @PreAuthorize("permitAll()")
    @ApiOperation("查询所有类别")
    @GetMapping("/all")
    public ResponseVo<List<Category>> queryAllCategory(){
        return categoryService.queryAllCategory();
    }

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("创建类别")
    @PostMapping("/insert")
    public ResponseVo<Map<String, Integer>> createCategory(@RequestParam String category){
        return categoryService.createCategory(category);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("删除类别")
    @DeleteMapping("/deleteCategory/{categoryId}")
    public ResponseVo<Category> deleteCategory(@PathVariable Integer categoryId) {
        return categoryService.deleteCategory(categoryId);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("更新标签")
    @PutMapping("/updateCategory")
    public ResponseVo<Category> updateCategory(@RequestBody Category category) {
        return categoryService.updateCategory(category);
    }
}
